#include"ThreadPool.h"


int exitPipe[2];
void handler(int signum)
{
	printf("signum = %d\n", signum);
	write(exitPipe[1], "1", 1);
}
int main()
{

	pipe(exitPipe);
	if (fork()) { // Parent process
		close(exitPipe[0]);
		signal(SIGUSR1, handler);
        while(1)
        {
            pause();
        }
        int status;
        wait(&status);
        if (WIFEXITED(status)) {
            printf("Child exited with status %d\n", WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("Child terminated by signal %d\n", WTERMSIG(status));
        }
        printf("Parent is going to exit!\n");
        exit(0);
    }
	close(exitPipe[1]);
	printf("get start~!\n");
	ThreadPool threadpool;
	initThreadPool(&threadpool);
	if (initThreadPool(&threadpool) != 0) {
		fprintf(stderr, "Thread pool initialization failed\n");
		exit(1);
	}
	printf("get threadpool done\n");
	int sockfd;
	initTcp(&sockfd, getKey(threadpool._map, "ip"),getKey(threadpool._map, "port"));
	ERROR_CHECK(sockfd, -1, "socket create error");
	printf("sever start success!On Port:%s\n",getKey(threadpool._map, "port"));

	int epfd = epoll_create(1);
	ERROR_CHECK(epfd, -1, "EPOLL CREATE ERROR");
	epollAdd(epfd, sockfd);
	epollAdd(epfd, exitPipe[0]);

	struct epoll_event readyset[1024];
	while (1) {
		int readyNum = epoll_wait(epfd, readyset, 1024, -1);
		for (int i = 0; i < readyNum; i++) {
			int currentFd = readyset[i].data.fd;
			if (currentFd == sockfd) {
				int netfd = accept(sockfd, NULL, NULL);
				printf("now a new client  connect: %d\n", netfd);
				epollAdd(epfd, netfd);
			} else if (currentFd == exitPipe[0]) {
				printf("Child is going to exit!\n");
				pthread_mutex_lock(&threadpool._mThreadLock);
				threadpool._exitFlag = 1;
                pthread_cond_broadcast(&threadpool._mThreadCond);
                pthread_mutex_unlock(&threadpool._mThreadLock);
                for(int i = 0 ;i<threadpool._thread_num;i++)
                {
                    pthread_join(threadpool._poolThreadId[i],NULL);
                }
                close(sockfd);
                close(epfd);
                printf("All task is done！Child has exit\n");
                exit(0);
            }else
            {
                int netfd = currentFd;
                int res = doRequest(netfd,&threadpool);
                if(res == CMD_TYPE_SYNC_PUTS || res ==CMD_TYPE_SYNC_GET)
                {
                    epollDel(epfd,netfd);
                    continue;
                }else if(res == -1)
                {

                }
            }
        }
    }
    
}

// 	int sockfd;
//     initTcp(&sockfd,get(threadpool._map,"ip"),get(threadpool._map,"port"));
// 	ERROR_CHECK(sockfd, -1, "socket create error");
// 	int epfd = epoll_create(1);

// 	epollAdd(epfd, sockfd);
// 	epollAdd(epfd, exitPipe[0]); // 监听退出管道

// 	struct epoll_event readyset[1024];
// 	while (1) {
// 		int readyNum = epoll_wait(threadpool.epfd, readyset, 1024, -1);
// 		for (int i = 0; i < readyNum; i++) {
// 			int currentFd = readyset[i].data.fd;
// 			if (currentFd == sockfd) {
// 				// 处理新连接
// 				int netfd = accept(sockfd, NULL, NULL);
// 				printf("netfd : %d\n", netfd);
// 				epollAdd(epfd, netfd);
// 			} else if (currentFd == exitPipe[0]) {
// 				// 处理退出信号
// 				printf("Child is going to exit!\n");
// 				pthread_mutex_lock(&threadpool._mLock);
// 				threadpool._exitFlag = 1;
// 				// 清理未处理的任务
// 				while (!_taskQueue_is_empty(
// 					&threadpool._taskQueue)) {
// 					task_t *task = _taskQueue_front(
// 						&threadpool._taskQueue);
// 					_taskQueue_deque(
// 						&threadpool._taskQueue);
// 					close(task->_clientFd);
// 					free(task);
// 				}
// 				pthread_cond_broadcast(&threadpool._mCond);
// 				pthread_mutex_unlock(&threadpool._mLock);
// 				// 等待所有线程退出
// 				for (int i = 0;
// 				     i < threadpool._tidArr._worker_num; ++i) {
// 					pthread_join(threadpool._tidArr._arr[i],
// 						     NULL);
// 				}
// 				// 关闭资源
// 				close(sockfd);
// 				close(threadpool.epfd);
// 				printf("Child has exited!\n");
// 				exit(0);
// 			} else {
// 				// 处理客户端数据
// 				int netfd = currentFd;
// 				int res = doRequest(netfd,&threadpool);
//                 if(res == )
// 			}
// 		}
// 	}
// 	return 0;
// }
